import { defineStore } from "pinia";
import type { Ref } from "vue";
import { ref } from "vue";
interface iUser {
    token: string
    userid: string
    isLogin: boolean
    usertel: string
    userpsw: string
    username:string
}
export const useUserStore = defineStore('user', () => {
    //存放参数，这个参数在登录成功之后res返回体里面
    const token: Ref<string> = ref(localStorage.getItem('token') || '')
    const userid: Ref<string> = ref(localStorage.getItem('userid') || '')
    const username: Ref<string> = ref(localStorage.getItem('username') || '用户123456')//用户名
    const usertel: Ref<string> = ref(localStorage.getItem('usertel') || '')
    const isLogin: Ref<boolean> = ref(localStorage.getItem('isLogin') == 'true')//默认设置未登录
    const userpsw: Ref<string> = ref(localStorage.getItem('token') || '123456')
    function changeUserState(userInfo: iUser) {
        token.value = userInfo.token
        userid.value = userInfo.userid
        usertel.value = userInfo.usertel
        userpsw.value = userInfo.userpsw
        username.value = userInfo.username
        isLogin.value = userInfo.isLogin
        //把数据存在本地
        localStorage.setItem('token', userInfo.token)
        localStorage.setItem('username', userInfo.username)
        localStorage.setItem('userid', String(userInfo.userid))
        localStorage.setItem('usertel', userInfo.usertel)
        localStorage.setItem('userpsw', userInfo.userpsw)
        localStorage.setItem('isLogin', String(userInfo.isLogin))
    }

    return {
        token,
        userid,
        usertel,
        userpsw,
        username,
        isLogin,
        changeUserState
    }
})